package com.laker.admin.module.invoice.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.laker.admin.framework.aop.metrics.Metrics;
import com.laker.admin.framework.model.PageResponse;
import com.laker.admin.framework.model.Response;
import com.laker.admin.module.invoice.entity.Invoice;
import com.laker.admin.module.invoice.service.InvoiceService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/invoice")
@Metrics
public class InvoiceController {

    @Autowired
    InvoiceService invoiceService;


    @GetMapping("/list")
    @ApiOperation(value = "分页查询")
    public PageResponse pageAll(@RequestParam(required = false, defaultValue = "1") long page,
                                @RequestParam(required = false, defaultValue = "10") long limit,
                                String keyWord) {

        Page roadPage = new Page<>(page, limit);
        LambdaQueryWrapper<Invoice> queryWrapper = new QueryWrapper().lambda();

        queryWrapper.and(StrUtil.isNotBlank(keyWord), i -> i.
                like(StrUtil.isNotBlank(keyWord), Invoice::getPhone, keyWord)
                .or().like(StrUtil.isNotBlank(keyWord), Invoice::getUserName, keyWord))
                .orderByDesc(Invoice::getId);

        Page pageList = invoiceService.page(roadPage, queryWrapper);

        return PageResponse.ok(pageList.getRecords(), pageList.getTotal());
    }

    @PostMapping("/saveOrUpdate")
    @ApiOperation(value = "新增或者更新")
    @Transactional
    @SaCheckPermission("invoice.update")
    public Response SaveOrUpdate(@RequestBody Invoice param) {
        if (param.getId() != null) {
            param.setUpdateTime(LocalDateTime.now());
        }else {
            param.setCreateTime(LocalDateTime.now());
            param.setUpdateTime(LocalDateTime.now());
        }

        return Response.ok(invoiceService.saveOrUpdate(param));
    }
    @GetMapping("/show/{id}")
    @ApiOperation(value = "根据id查询")
    public Response get(@PathVariable Long id) {
        return Response.ok(invoiceService.getById(id));
    }

    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "根据id删除")
    @SaCheckPermission("invoice.delete")
    public Response delete(@PathVariable Long id) {
        return Response.ok(invoiceService.removeById(id));
    }

    @DeleteMapping("/del/batch/{ids}")
    @ApiOperation(value = "根据批量删除ids删除")
    @SaCheckPermission("invoice.delete")
    public Response batchRemove(@PathVariable Long[] ids) {
        return Response.ok(invoiceService.removeByIds(CollUtil.toList(ids)));
    }
}
